<?php

/**
 * MVM_MALL 网上商店系统  配送区域管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-02-03 $
 * $Id: area.inc.php  www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if ($action=='list' && is_numeric($ship_id)) {
    $ship       = $db->get_one("SELECT id,name FROM  `{$tablepre}ship_table` WHERE id = '$ship_id'");
    $result     = $db->query("SELECT * FROM `{$tablepre}area_table`  WHERE ship_id='$ship_id' ");
    while ($rt  = $db->fetch_array($result)){
        $rt['edit']  = "admincp.php?module=$module&action=edit&ship_id=$rt[ship_id]&area_id=$rt[area_id]";
		$rt['del']   = "admincp.php?module=$module&action=del&area_id=$rt[area_id]";
        $area[]      = $rt;
    }
    require_once template('area');
    footer();
} elseif ($action=='add' && is_numeric($ship_id)) {
    if($setp==1 && $_POST){
        $area_name = dhtmlchars($area_name);
        $region    = implode(',',$region);
        $area_nums = $db->counter("{$tablepre}area_table","where ship_id='$ship_id' and area_name='$area_name'");
        if ($area_nums>0){
            admin_msg('该区域已经存在');
        }
        $ship   = $db->get_one("SELECT id,class_name FROM `{$tablepre}ship_table` WHERE id='$ship_id'");
        require_once MVMMALL_ROOT.'./include/shipping/'.$ship['class_name'].'.class.php';
        $config = array();
        foreach ($shipping[$ship['class_name']]['cfg'] AS $key => $val)
        {
            $config[$key]['name']   = $val['name'];
            $config[$key]['value']  = $$val['name'];
            $config[$key]['label']  = $val['label'];
        }
        $count                      = count($config);
        $config[$count]['name']     = 'free_money';
        $config[$count]['value']    = $free_money;
        $config[$count]['label']    = '免费额度';
        /* 如果支持货到付款，则允许设置货到付款支付费用 */
        if ($shipping[$ship['class_name']]['cod'])
        {
            $count++;
            $config[$count]['name']     = 'pay_fee';
            $config[$count]['value']    = $pay_fee;
            $config[$count]['label']    = '货到付款支付费用';
        }
        $config = serialize($config);
        $query = "INSERT INTO `{$tablepre}area_table` SET
                  area_name  = '$area_name',
                  ship_id    =  '$ship_id',
                  config     =  '$config',
                  region     =  '$region'
                 ";
        $db->query($query);
        admin_msg('success',"admincp.php?module=area&action=list&ship_id=$ship_id");

    } else {
        $ship   = $db->get_one("SELECT * FROM `{$tablepre}ship_table` WHERE id = '$ship_id'");
        require_once MVMMALL_ROOT.'./include/shipping/'.$ship['class_name'].'.class.php';
        $fields = array();
        foreach ($shipping[$ship['class_name']]['cfg'] AS $key => $val)
        {
            $fields[$key]['name']   = $val['name'];
            $fields[$key]['value']  = $val['value'];
            $fields[$key]['label']  = $val['label'];
        }
        $count = count($fields);
        $fields[$count]['name']     = 'free_money';
        $fields[$count]['value']    = '0';
        $fields[$count]['label']    = '免费额度';
        /* 如果支持货到付款，则允许设置货到付款支付费用 */
        if ($shipping[$ship['class_name']]['cod'])
        {
            $count++;
            $fields[$count]['name']     = 'pay_fee';
            $fields[$count]['value']    = '0';
            $fields[$count]['label']    = '货到付款支付费用';
        }
        require_once template('area_add');
        footer();
    }
} elseif ($action=='edit' && is_numeric($area_id)) {
    if($setp==1 && $_POST){
        $area_name = dhtmlchars($area_name);
        $region    = implode(',',$region);
        $area_nums = $db->counter("{$tablepre}area_table","where ship_id='$ship_id' AND area_name='$area_name' AND area_id <> '$area_id'");
        if ($area_nums>0){
            admin_msg('该区域已经存在');
        }
        $ship   = $db->get_one("SELECT id,class_name FROM `{$tablepre}ship_table` WHERE id='$ship_id'");
        require_once MVMMALL_ROOT.'./include/shipping/'.$ship['class_name'].'.class.php';
        $config = array();
        foreach ($shipping[$ship['class_name']]['cfg'] AS $key => $val)
        {
            $config[$key]['name']   = $val['name'];
            $config[$key]['value']  = $$val['name'];
            $config[$key]['label']  = $val['label'];
        }
        $count = count($config);
        $config[$count]['name']     = 'free_money';
        $config[$count]['value']    = $free_money;
        $config[$count]['label']    = '免费额度';
        /* 如果支持货到付款，则允许设置货到付款支付费用 */
        if ($shipping[$ship['class_name']]['cod'])
        {
            $count++;
            $config[$count]['name']     = 'pay_fee';
            $config[$count]['value']    = $pay_fee;
            $config[$count]['label']    = '货到付款支付费用';
        }
        $config = serialize($config);
        $query = "UPDATE `{$tablepre}area_table` SET
                  area_name  = '$area_name',
                  ship_id    =  '$ship_id',
                  config     =  '$config',
                  region     =  '$region'
                  WHERE area_id = '$area_id'";
        $db->query($query);
        admin_msg('success',"admincp.php?module=area&action=list&ship_id=$ship_id");

    } else {
        $area   = $db->get_one("SELECT * FROM `{$tablepre}area_table` WHERE area_id = '$area_id'");
        $fields = unserialize($area['config']);
        /* 如果配送方式支持货到付款并且没有设置货到付款支付费用，则加入货到付款费用 */
        if ($area['cod'] && $fields[count($fields)-1]['name'] != 'pay_fee')
        {
            $fields[]   = array('name'=>'pay_fee', 'value'=>0,'label'=>'货到付款支付费用');
        }
        $area['region'] = explode(',',$area['region']);
        require_once template('area_add');
        footer();
    }
} elseif ($action=='del' && is_numeric($area_id)) {
    $db->query("DELETE FROM `{$tablepre}area_table` WHERE area_id = '$area_id'");
    admin_msg('success',dreferer());
} else {
    admin_msg('pass_worng');
}